//
//  RcvAdressViewController.m
//  DuBai
//
//  Created by darktm on 14-3-11.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "RcvAdressViewController.h"

#import "RcvAddressAddViewController.h"

#import "RcvAdressViewCell.h"

#import "TaskWithGetAddressList.h"
#import "TaskWithDelAddress.h"

#import "Addresses.h"

@interface RcvAdressViewController ()<WYHttpTaskDelegate,RcvAddressAddViewControllerDelegate>

@property (nonatomic,strong)NSMutableArray *addressArray;

@end

@implementation RcvAdressViewController

#define TAG_CELL_DEL_BTN 10000


@synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.editBtn.layer.cornerRadius = 13.0f;
    
    [self loadAddresses];
}

#pragma mark - tableview delegate -

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view =[[UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.addressArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RcvAdressViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RcvAdressViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if(indexPath.row == 0){
        cell.defaultLabel.hidden = NO;
        cell.delBtn.hidden = YES;
    }else{
        cell.defaultLabel.hidden = YES;
        cell.delBtn.hidden = NO;
    }
    cell.delBtn.tag = TAG_CELL_DEL_BTN + indexPath.row;
    [cell.delBtn addTarget:self action:@selector(on_btn_del:) forControlEvents:UIControlEventTouchUpInside];
    
    Addresses *obj = self.addressArray[indexPath.row];
    
    cell.phone.text = obj.telephone;
    cell.address.text = obj.address;
    cell.name.text = obj.consignee;
    cell.postcode.text = obj.email;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(delegate && [delegate respondsToSelector:@selector(rcvAdressDidSelectedInfo:)]){
        [delegate rcvAdressDidSelectedInfo:self.addressArray[indexPath.row]];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        RcvAddressAddViewController *a = (RcvAddressAddViewController *)[Utils storyBoardWithId:@"RcvAddressAddViewController"];
        a.address = self.addressArray[indexPath.row];
        a.delegate = self;
        [self.navigationController pushViewController:a animated:YES];
    }
}


#pragma mark - http delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    id rsult = [obj parese];
    
    if([obj isKindOfClass:[TaskWithGetAddressList class]]){
        self.addressArray = rsult;
        [self.tableView reloadData];
    }else if([TaskWithDelAddress class]){
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }
}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    DLog(@"%@",obj.networkOp.url);
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender NS_AVAILABLE_IOS(5_0){
    DLog(@"%@",segue.identifier);
    if([segue.identifier isEqualToString:@"RcvAddAddress"]){
        RcvAddressAddViewController *rcv = segue.destinationViewController;
        rcv.delegate = self;
    }
}


#pragma mark - add address delegate -

-(void)rcvAddressDidEnd{
    
    [self loadAddresses];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - private method -

-(void)loadAddresses{
    
    //是否有分页
    if(self.addressArray){
        [self.addressArray removeAllObjects];
    }else{
        self.addressArray = [NSMutableArray array];
    }
    
    TaskWithGetAddressList *list = [[TaskWithGetAddressList alloc] init];
    list.delegate = self;
    [list start];
}

-(void)on_btn_del:(UIButton *)btn{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    int tag = btn.tag - TAG_CELL_DEL_BTN;
    Addresses *a = self.addressArray[tag];
    
    TaskWithDelAddress *del = [[TaskWithDelAddress alloc] init];
    del.addressid = a.id;
    del.delegate = self;
    [del start];
}
- (IBAction)on_btn_edit:(UIButton *)sender {
}
@end
